/**
 * Action的基类
 */

package cn.antia.cwin.page;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.*;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.config.DefaultSettings;

//import com.opensymphony.webwork.WebWorkStatics;
//import com.opensymphony.webwork.config.DelegatingConfiguration;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.antia.cep.common.*;
import cn.antia.util.ServletURLUtils;
/**
 * @author bin
 * @version 0.1
 * @date Sep 29, 2006
 */
public abstract class StrutsAction extends ActionSupport{
	public static final int  ADD = 1;
    public static final int  UPDATE = 2;
    public static final int  DELETE = 4;
    public static final int  QUERY = 5;
    protected HttpServletRequest request = getHttpServletRequest();
    protected HttpSession session;
    
	protected List messages = new LinkedList(); // 错误信息列表
	protected boolean isOccurError = false; // 是否发生错误

	protected void init() {
		request = getHttpServletRequest();
		session = request == null ? null : request.getSession();
	}
	
	@Override
	public String execute() {
		init();
		try {
			if (checkSession(getHttpServletRequest().getSession())) {
				return action();
			} else {
				// session失效，请重新登录
				javax.servlet.RequestDispatcher rd = null;
				rd = request.getRequestDispatcher("/login.jsp");
				try {
					request.setAttribute("message", "SESSION已经失效，请重新登录.");
					rd.forward(request, getHttpServletResponse());
				} catch (Exception ex) {
				}
			}
		} catch (AntiaException e) {
			AntiaLog.error(e.getMessage());
			request.setAttribute("error_msg", e.getMessage());
			return "error";
		} catch (Exception e) {
			e.printStackTrace();
			AntiaLog.error(e.getMessage());
			request.setAttribute("error_msg", "对不起，系统出现故障，请等待故障排除...");
			return "error";
		}
		return "error";
	}
    
    protected abstract String action() throws AntiaException;

	/**
	 * 获得当前Http Servlet Request
	 */
	public HttpServletRequest getHttpServletRequest() {
		return (HttpServletRequest) ActionContext.getContext().get(
				StrutsStatics.HTTP_REQUEST);
	}
	
	/**
	 * 获得 Http Servlet Response
	 */
	public HttpServletResponse getHttpServletResponse() {
		return (HttpServletResponse) ActionContext.getContext().get(
				StrutsStatics.HTTP_RESPONSE);
	}

	/**
	 * 获得当前Action 请求路径
	 * 
	 */
	public String getRequestUrl() {
		return ServletURLUtils.getRequestUrl(getHttpServletRequest());
	}

	/**
	 * 获得当前Action 请求的全部路径
	 */
	public String getFullRequestUrl() {
		return ServletURLUtils.getFullRequestUrl(getHttpServletRequest());
	}

	/**
	 * 获得当前Session
	 * 
	 */
	public Map getSession() {
		return ActionContext.getContext().getSession();
	}

	/**
	 * 根据指定的页面参数名称，获取页面传递来的参数值
	 * 
	 * @param parameter
	 * @return 单个对象
	 */
	protected Object getParameterValue(String parameter) {
		Object[] parameterArray = getParamenterArray(parameter);
		if (parameterArray != null && parameterArray.length == 1) {
			return parameterArray[0];
		} else {
			return getHttpServletRequest().getAttribute(parameter);
		}
	}

	/**
	 * 根据指定的页面参数名称，获取页面传递来的参数值
	 * 
	 * @param parameter
	 * @return 数组对象
	 */
	protected Object[] getParamenterArray(String parameter) {
		return (Object[]) (ActionContext.getContext().getParameters()
				.get(parameter));
	}

	/**
	 * 获取配置信息，来源为webwork.properties以及webwork.properties中<br>
	 * webwork.custom.properties指定的properties文件<br>
	 * 暂未使用
	 */
	protected String getApplicationProperty(String key) {
		return DefaultSettings.get(key);
	}
	
	/**
	 * 向request对象添加attribute
	 * 
	 * @param key
	 * @param value
	 */
	public void setRequestAttribute(String key, Object value) {
		getHttpServletRequest().setAttribute(key, value);
	}

	/**
	 * 从session中取得相应的值
	 * 
	 * @param key
	 * @return
	 */
	public Object getSessionObj(Object key) {
		return getSession().get(key);
	}
	
	/**
	 * 获得项目rootDir
	 * 
	 * @return
	 */
	protected String getRootDir() {
		String workDir = getHttpServletRequest().getSession()
				.getServletContext().getRealPath("login.jsp");

		int len = workDir.indexOf("login.jsp");
		workDir = workDir.substring(0, len);

		StringBuffer sb = new StringBuffer();
		sb.append(workDir).append("upload").append(File.separator);

		return sb.toString();
	}
	
	@SuppressWarnings("unchecked")
	public void setSessionObj(Object key,Object value){
		getSession().put(key, value);
	}

	public boolean checkSession(HttpSession session) {
		return !(session.getAttribute(SessionConst.PRIVILEGE) == null);
	}
	
}
